﻿using System;
using Space2D_Utils;

namespace Space2D_Core
{
	[Serializable]
	public class PhysicalObject : AABB
	{
		public Action<PhysicalObject> EventPosChanged;

		public PhysicalObject(string name, double mass)
		{
			Name = name;
			Mass = mass;
		}

		public virtual void Init(Vector2d acceleration, Vector2d speed, Vector2d position)
		{
			Acceleration = new Vector2d(acceleration.X, acceleration.Y);
			Speed = new Vector2d(speed.X, speed.Y);
			Position = new Vector2d(position.X, position.Y);
		}

		public virtual void Tick(int ms)
		{
			double dms = ms / 1000.0;

			Vector2d acc = GetTotalAcceleration();

			Position += Speed * dms + acc * (dms * dms / 2.0);
			Speed += acc * dms;

			if (EventPosChanged != null)
			{
				EventPosChanged(this);
			}
		}

		public virtual Vector2d GetTotalAcceleration()
		{
			return Acceleration;
		}

		public void AddAcceleration(Vector2d acceleration)
		{
			Acceleration += acceleration;
		}

		public void SetAcceleration(Vector2d acceleration)
		{
			Acceleration = acceleration;
		}

		public override string ToString()
		{
			return "Name " + Name + "; Mass " + Mass + "; Acceleration " + Acceleration + "; Speed " +
				Speed + "; Position" + Position;
		}

		public string Name { get; protected set; }
		public double Mass { get; protected set; }
		public Vector2d Acceleration { get; private set; }
		public Vector2d Speed { get; private set; }
	}
}
